﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCEntity.OA.TeamWorks.TaskRecord;
using Dapper;
using BCCommon;
namespace BCData.OA.TeamWorks.TaskRejectReplyRecord
{
    public class TaskRejectReplyRecordData: ITaskRejectReplyRecordData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT TaskRejectReplyRecordId,TaskRejectRecordId,ReplyContent,ReplyTime,ReplyAdminId,ReplyFullName,WorkflowActivityInstanceId FROM OA_TaskRejectReplyRecord";
        public TaskRejectReplyRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public async Task<TaskRejectReplyRecordEntity> Add(TaskRejectReplyRecordEntity recordEntity)
        {
            string sql = SqlCommands.GetInsertSql("OA_TaskRejectReplyRecord", new string[] { "TaskRejectRecordId", "ReplyContent", "ReplyTime", "ReplyAdminId", "ReplyFullName", "WorkflowActivityInstanceId" });
            sql = string.Format("{0} {1} WHERE TaskRejectReplyRecordId=LAST_INSERT_ID()", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TaskRejectRecordId", recordEntity.TaskRejectRecordId, DbType.Int64, size: 20);
            parameters.Add("?ReplyContent", recordEntity.ReplyContent, DbType.String);
            parameters.Add("?ReplyTime", recordEntity.ReplyTime, DbType.DateTime);
            parameters.Add("?ReplyAdminId", recordEntity.ReplyAdminId, DbType.Int64, size: 20);
            parameters.Add("?ReplyFullName", recordEntity.ReplyFullName, DbType.AnsiString, size: 20);
            parameters.Add("?WorkflowActivityInstanceId", recordEntity.WorkflowActivityInstanceId, DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<TaskRejectReplyRecordEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<IEnumerable<TaskRejectReplyRecordEntity>> GetEntities(long workflowActivityInstanceId, long taskRejectRecordId)
        {
            string sql = string.Format("{0} WHERE TaskRejectRecordId=?TaskRejectRecordId AND WorkflowActivityInstanceId=?WorkflowActivityInstanceId",QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TaskRejectRecordId", taskRejectRecordId, DbType.Int64, size: 20);
            parameters.Add("?WorkflowActivityInstanceId", workflowActivityInstanceId, DbType.AnsiString, size: 20);
            return await this.databaseContext.Slave.QueryAsync<TaskRejectReplyRecordEntity>(sql, parameters);
        }
    }
}
